# encoding=utf-8
from LBT.BaseParent.BaseLbt import lbtlogin
from LBT.Resource.ForgetAtt import ForgetAtt
from LBT.Resource.WelcomeAtt import WelcomeAtt
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait


class welcome(lbtlogin,ForgetAtt,WelcomeAtt):
    '''打开页面后首页的用例，包括功能介绍、
    '''
    @lbtlogin.catchfun
    def test_login(self):
        '''
        学校管理页面--通过检查用户名控件+url方式确定用例是否正确
        张志莹--20190318
        20190329--duvee
        :return:
        '''
        self.docstr='学校管理页面--通过检查用户名控件+url方式确定用例是否正确'
        self.openUrl()
        self.driver.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()
        userdisplay = self.driver.find_element_by_id(self.lbt_welcome_mainpage_login_usernameid).is_displayed()
        self.assertTrue(userdisplay, self.lbt_welcome_mainpage_login_errormessage)
        lurl = self.driver.current_url
        self.assertEqual(self.urlForLbtLogin + self.lbt_welcome_mainpage_login_url, lurl)

    @lbtlogin.catchfun
    def test_mainpage(self):
        '''
        我是登录网站的首页--通过首页控件class+url判断是否正确
        张志莹--20190322
        20190329--duvee
        :return:
        '''
        self.docstr ='我是登录网站的首页--通过首页控件class+url判断是否正确'
        self.openUrl()
        getInfo = self.driver.find_element_by_class_name(self.lbt_welcome_mainpage_class_name)
        self.assertIn(self.lbt_welcome_mainpage_text, getInfo.text)
        murl = self.driver.current_url
        self.assertIn(self.urlForLbtLogin + self.lbt_welcome_mainpage_url, murl)

    @lbtlogin.catchfun
    def test_introduce(self):
        '''
        功能介绍页面--通过id和url判断页面加载正确
        张志莹--20190322
        20190329duvee
        :return:
        '''
        self.docstr ='功能介绍页面--通过id和url判断页面加载正确'
        self.openUrl()
        # self.driver.find_element_by_link_text('功能介绍').click()
        self.driver.find_element_by_xpath(self.lbt_welcome_mainpage_introduce_xpath).click()
        isdisplay = self.driver.find_element_by_id(self.lbt_welcome_mainpage_introduce_id).is_displayed()
        self.assertTrue(isdisplay,self.lbt_welcome_mainpage_introduce_errormessage)
        iurl = self.driver.current_url
        self.assertIn(self.urlForLbtLogin + self.lbt_welcome_mainpage_introduce_url, iurl)

    @lbtlogin.catchfun
    def test_apply(self):
        '''
        申请使用页面--通过id和url判断页面加载正确
        张志莹--20190322
        20190329duvee
        :return:
        '''
        self.docstr ='申请使用页面--通过id和url判断页面加载正确'
        self.openUrl()
        self.driver.find_element_by_xpath(self.lbt_welcome_mainpage_apply_xpath).click()
        text = self.driver.find_element_by_id(self.lbt_welcome_mainpage_apply_id).is_displayed()
        self.assertTrue(text, self.lbt_welcome_mainpage_apply_errormessage)
        aurl = self.driver.current_url
        self.assertIn(self.urlForLbtLogin + self.lbt_welcome_mainpage_apply_url, aurl)

    @lbtlogin.catchfun
    def test_join(self):
        '''
        加盟合作页面--通过class和url判断页面加载正确
        张志莹--20190322
        20190329duvee
        :return:
        '''
        self.docstr ='加盟合作页面--通过class和url判断页面加载正确'
        self.openUrl()
        # self.driver.find_element_by_link_text('加盟合作').click()
        self.driver.find_element_by_xpath(self.lbt_welcome_mainpage_join_xpath).click()
        infoGet = self.driver.find_element_by_class_name(self.lbt_welcome_mainpage_join_class_name).is_displayed()
        self.assertTrue(infoGet, self.lbt_welcome_mainpage_join_errormessage)
        ourl = self.driver.current_url
        self.assertIn(self.urlForLbtLogin + self.lbt_welcome_mainpage_join_url, ourl)

    @lbtlogin.catchfun
    def test_aboutus(self):
        '''
        关于我们页面--通过class和url判断页面正确
        张志莹--20190322
        20190329duvee
        :return:
        '''
        self.docstr ='关于我们页面--通过class和url判断页面正确'
        self.openUrl()
        # self.driver.find_element_by_link_text('关于我们').click()
        self.driver.find_element_by_xpath(self.lbt_welcome_mainpage_aboutus_xpath).click()
        getInfo = self.driver.find_element_by_class_name(self.lbt_welcome_mainpage_aboutus_class_name)
        self.assertEqual(getInfo.text, self.lbt_welcome_mainpage_aboutus_errormessage)
        aurl = self.driver.current_url
        self.assertIn(self.urlForLbtLogin + self.lbt_welcome_mainpage_aboutus_url, aurl)

    def forgetPass(self):
        '''公共的忘记密码部分==含验证页面及切换窗口
        duvee20190329
        '''
        self.openUrl()
        self.driver.find_element_by_xpath(self.lbt_welcome_mainpage_login_xpath).click()
        # 验证是否进入登录首页
        dr = self.driver
        title = dr.find_element_by_class_name(self.lbt_welcome_mainpage_login_denglukuang_class).text
        self.assertEqual(title, self.lbt_welcome_mainpage_login_title)
        oldwindows = dr.window_handles
        dr.find_element_by_xpath(self.lbt_welcome_mainpage_login_forgetpass_xpath).click()
        sleep(1)
        newwdindows = dr.window_handles
        self.switch_newwindowhandle(dr, oldwindows, newwdindows)
        # 验证页面跳转是否正确
        currentUrl = dr.current_url
        text = dr.find_element_by_xpath(self.lbt_forget_url_xpath).text
        self.assertTrue(currentUrl.__contains__(self.lbt_forget_url))
        self.assertEqual(text, self.lbt_forget_textAss)

    @lbtlogin.catchfun
    def test_noVerification(self):
        '''忘记密码--不填写验证码
        杨大伟
        '''
        self.docstr='忘记密码--不填写验证码'
        self.forgetPass()
        dr = self.driver
        dr.find_element_by_id(self.forget_first_phone_id).clear()
        dr.find_element_by_id(self.forget_first_btn_id).click()
        text = dr.find_element_by_id(self.forget_first_phoneTip_id).text
        self.assertEqual(self.forget_first_phoneTip_text, text)

    @lbtlogin.catchfun
    def test_errorVerification(self):
        '''忘记密码--错误验证码--杨大伟'''
        self.docstr ='忘记密码--错误验证码'
        self.forgetPass()
        dr = self.driver
        dr.find_element_by_id(self.forget_first_phone_id).clear()
        dr.find_element_by_id(self.forget_first_phone_id).send_keys(self.lbtUserName)
        dr.find_element_by_id(self.forget_first_codeInput_id).clear()
        dr.find_element_by_id(self.forget_first_codeInput_id).send_keys("aaaa"+self.lbtUserPassword)
        dr.find_element_by_id(self.forget_first_btn_id).click()
        text = dr.find_element_by_id(self.forget_first_codeTip_id).text
        self.assertEqual(self.forget_first_codeTip_text, text)

    @lbtlogin.catchfun
    def test_errorphone(self):
        '''忘记密码--错误手机号--杨大伟'''
        self.docstr ='忘记密码--错误手机号'
        self.forgetPass()
        dr = self.driver
        dr.find_element_by_id(self.forget_first_phone_id).clear()
        dr.find_element_by_id(self.forget_first_phone_id).send_keys(self.forget_first_noExistPhone)
        # 点击一下页面其他地方，让用户名输入框失去焦点
        dr.find_element_by_id(self.forget_first_phoneTip_id).click()
        ele = dr.find_element_by_id(self.forget_first_codeInput_id)
        dr.execute_script("arguments[0].focus();", ele)
        yanzhengma = self.yanzhengma(dr, dr.find_element_by_id(self.forget_first_code_id))
        dr.find_element_by_id(self.forget_first_codeInput_id).click()
        dr.find_element_by_id(self.forget_first_codeInput_id).clear()
        dr.find_element_by_id(self.forget_first_codeInput_id).send_keys(yanzhengma)
        dr.find_element_by_id(self.forget_first_btn_id).click()
        text = dr.find_element_by_id(self.forget_first_phoneTip_id).text
        self.assertEqual(self.forget_first_phoneTip_textNoUser, text)

    def success_code(self):
        '''忘记密码---验证码正确并通过--杨大伟'''
        self.docstr ='忘记密码---验证码正确并通过'
        self.forgetPass()
        dr = self.driver
        # 验证码校验，可能一次不通过，只要没有跳转到第二个页面，就继续尝试，直到成功
        while not dr.find_element_by_id(self.forget_first_valid_id).value_of_css_property("display") == 'block':
            dr.find_element_by_id(self.forget_first_code_id).click()
            dr.find_element_by_id(self.forget_first_phone_id).clear()
            dr.find_element_by_id(self.forget_first_phone_id).send_keys(self.forget_first_ExistPhone)
            yanzhengma = self.yanzhengma(dr, dr.find_element_by_id(self.forget_first_code_id))
            dr.find_element_by_id(self.forget_first_codeInput_id).clear()
            dr.find_element_by_id(self.forget_first_codeInput_id).send_keys(yanzhengma)
            dr.find_element_by_id(self.forget_first_btn_id).click()
        text = dr.find_element_by_id(self.forget_first_phoneTip_id).text
        self.assertTrue(dr.find_element_by_id(self.forget_first_valid_id).value_of_css_property("display") == 'block')

    @lbtlogin.catchfun
    def test_errorSmsCode(self):
        '''忘记密码--正确验证码，错误短信验证码--杨大伟'''
        self.docstr ='忘记密码--正确验证码，错误短信验证码'
        self.success_code()
        dr = self.driver
        dr.find_element_by_id(self.forget_second_getsmscodebtn_id).click()
        WebDriverWait(dr, 10).until_not(
            lambda dr: dr.find_element_by_id(self.forget_second_getsmscodebtn_id).is_enabled())
        dr.find_element_by_id(self.forget_second_smscode_id).clear()
        dr.find_element_by_id(self.forget_second_smscode_id).send_keys("123456")
        dr.find_element_by_id(self.forget_second_nextcode_id).click()
        text = dr.find_element_by_id(self.forget_second_smscodeTip_id).text
        self.assertEqual(text, self.forget_second_smscodeTip_text)

    def getsmscode(self, phone):
        '''
        获取短信验证码--通过查询数据库--杨大伟
        :param phone:
        :return: 短信验证码
        '''
        sql = "select top 1 smscontent from lbt..smscode where phone ='" + phone + "' order by id desc"
        text = self.selectlbt(sql)
        print(text)
        text = text[0:6]
        print(text)
        return text

    def deleteOldSmsCode(self, phone):
        '''
        删除旧的该手机短信验证码数量--杨大伟
        :param phone:
        :return:
        '''
        sql = "delete from lbt..smscode where phone ='" + phone + "'"
        text = self.deleteAndUpdatelbt(sql)
        print(text)

    def newPassPage(self):
        '''
        进入到设置新密码的页面---并校验页面跳转正确--杨大伟
        :return:
        '''
        dr = self.driver
        self.success_code()
        self.deleteOldSmsCode(self.forget_first_ExistPhone)
        dr.find_element_by_id(self.forget_second_getsmscodebtn_id).click()
        WebDriverWait(dr, 10).until_not(
            lambda dr: dr.find_element_by_id(self.forget_second_getsmscodebtn_id).is_enabled())
        sleep(3)
        smscode = ''
        for i in range(10):
            sleep(1)
            if smscode.__len__() == 0:
                print("尝试获取短信验证码" + str(i))
                dr.find_element_by_id(self.forget_second_smscode_id).clear()
                smscode = self.getsmscode(self.forget_first_ExistPhone)
                dr.find_element_by_id(self.forget_second_smscode_id).send_keys(smscode)
            else:
                break
        dr.find_element_by_id(self.forget_second_nextcode_id).click()
        # 判断第三步页面调整是否正确
        self.assertTrue(dr.find_element_by_id(self.forget_third_id).value_of_css_property("display") == 'block',
                        msg='验证短信验证码失败，无法进入到下一步')

    @lbtlogin.catchfun
    def test_shuziPass(self):
        '''忘记密码--正确验证码，新密码为纯数字--杨大伟'''
        self.docstr ='忘记密码--正确验证码，新密码为纯数字'
        dr = self.driver
        # 进入到设置新密码页面
        self.newPassPage()
        dr.find_element_by_id(self.forget_third_newpass_id).clear()
        dr.find_element_by_id(self.forget_third_newpass_id).send_keys("123456789")
        dr.find_element_by_id(self.forget_third_newpassconfirm_id).clear()
        dr.find_element_by_id(self.forget_third_newpassconfirm_id).send_keys("123456789")
        dr.find_element_by_id(self.forget_third_btn_id).click()
        text = dr.find_element_by_id(self.forget_third_newpasstip_id).text
        self.assertEqual(text, self.forget_third_newpassTip_text)

    @lbtlogin.catchfun
    def test_twoPassNoSame(self):
        '''忘记密码--正确验证码，但是两次密码输入不一致--杨大伟'''
        self.docstr ='忘记密码--正确验证码，但是两次密码输入不一致'
        dr = self.driver
        # 进入到设置新密码页面
        self.newPassPage()
        dr.find_element_by_id(self.forget_third_newpass_id).clear()
        dr.find_element_by_id(self.forget_third_newpass_id).send_keys("abc666666")
        dr.find_element_by_id(self.forget_third_newpassconfirm_id).clear()
        dr.find_element_by_id(self.forget_third_newpassconfirm_id).send_keys("abc123456")
        dr.find_element_by_id(self.forget_third_btn_id).click()
        text = dr.find_element_by_id(self.forget_third_newpassconfirmTip_id).text
        self.assertEqual(text, self.forget_third_newpassTipconfirm_text)

    @lbtlogin.catchfun
    def test_VerificationSuccess(self):
        '''忘记密码--正确--杨大伟'''
        self.docstr ='忘记密码--正确'
        dr = self.driver
        # 进入到设置新密码页面
        self.newPassPage()
        dr.find_element_by_id(self.forget_third_newpass_id).clear()
        dr.find_element_by_id(self.forget_third_newpass_id).send_keys(self.sucesspass)
        dr.find_element_by_id(self.forget_third_newpassconfirm_id).clear()
        dr.find_element_by_id(self.forget_third_newpassconfirm_id).send_keys(self.sucesspass)
        dr.find_element_by_id(self.forget_third_btn_id).click()
        # 判断是否进入到了设置成功页面
        WebDriverWait(dr, 10).until(
            lambda dr: dr.find_element_by_xpath(self.forget_fourth_success_tip_xpath).is_displayed())
        text = dr.find_element_by_xpath(self.forget_fourth_success_tip_xpath).text
        self.assertEqual(text, self.forget_fourth_success_tip_text)
        # 返回到登录页面，使用新的密码，登录
        dr.find_element_by_xpath(self.forget_fourth_returnmainbtn_xpath).click()
        dr.find_element_by_id(self.login_user_id).clear()
        dr.find_element_by_id(self.login_user_id).send_keys(self.forget_first_ExistPhone)
        dr.find_element_by_id(self.login_pass_id).clear()
        dr.find_element_by_id(self.login_pass_id).send_keys(self.sucesspass)
        dr.find_element_by_id(self.login_loginbtn_id).click()
        # 判断是否进入到了选择学校页面
        self.assertTrue(dr.find_element_by_id(self.lbt_forget_mainpage_id).is_displayed())
